from django import forms
from categoriesapp.models import Category

def parent_selector():
    categories =  Category.objects.order_by('name')
    categories_choices = []
    for i in categories:
        categories_choices.append((i.id, i.name))
    categories_choices.append((0, "None"));
    categories_choices = tuple(categories_choices)
    return categories_choices

class CategoryForm(forms.Form):
    name = forms.CharField(max_length=100, label="Nome")
    
    #parent_options = parent_selector()
    
    #if (len(parent_options)):
    #    parent = forms.ChoiceField(parent_options, label="Aninhar na categoria")
        
    def save(self, cd):
        #if (cd["parent"] != '0'):
            #p = Category.objects.get(pk=cd["parent"])
            #new_category = CategoryChild(name = cd["name"], parent=p)
            #new_category.save()

        #else:
        new_category = Category(name = cd["name"])
        new_category.save()
                
        return new_category